package DaPing

import (
	"fmt"
	"github.com/gogf/gf/net/ghttp"
	"github.com/skip2/go-qrcode"
	"strconv"
	"text/app"
	"text/library/in"
	"time"
)

//查询大屏分布标点
func GetDaPingList(DisEnum int, size, index int) ([]*DaPingZuoBiao, int64) {
	list := []*DaPingZuoBiao{}
	session := app.DB.Limit(size, size*index).Desc("ID")
	if DisEnum > 0 {
		session.Where("DisEnum =?", DisEnum)
	}
	co, err := session.FindAndCount(&list)
	in.CheckErr(err)
	return list, co
}

//创建添加日志机制
func CreateLog(data *DaPingZuoBiao, name string, enum int) *DaPingZuoBiaoLog {
	model := new(DaPingZuoBiaoLog)
	model.DisEnum = data.DisEnum
	model.InDate = data.InDate
	model.TagName = data.TagName
	model.Name = name
	switch data.DisEnum {
	case 1:
		model.DisEnumName = "低洼地带"
	case 2:
		model.DisEnumName = "老人分布"
	case 3:
		model.DisEnumName = "留守儿童"
	case 4:
		model.DisEnumName = "民情地图"
	case 5:
		model.DisEnumName = "工程地图"
	default:
		model.DisEnumName = "低洼地带"
	}
	model.InUserID = data.InUserID
	fmt.Println(enum)
	switch enum {
	case 1:
		model.Operation = "创建添加日志"
	case 2:
		model.Operation = "创建修改日志"
	}

	return model
}

//创建投票日志机制
func CreateVote(data *DaPingZuoBiao, name string) *DaPingZuoVote {
	model := new(DaPingZuoVote)
	model.ZuoBiaoID = data.ID
	model.DisEnum = data.DisEnum
	model.InDate = data.InDate
	model.TagName = data.TagName
	model.Name = name
	switch data.DisEnum {
	case 1:
		model.DisEnumName = "低洼地带"
	case 2:
		model.DisEnumName = "老人分布"
	case 3:
		model.DisEnumName = "留守儿童"
	case 4:
		model.DisEnumName = "民情地图"
	case 5:
		model.DisEnumName = "工程地图"
	default:
		model.DisEnumName = "低洼地带"
	}
	model.InUserID = data.InUserID
	model.Operation = "创建投票日志"
	return model
}

//每个人只能投同一景点一次
func UpdateVote(r *ghttp.Request, ZuoBiaoID int64, Name string) {
	//查询投票日志里是否有投票
	dataVote := new(DaPingZuoVote)
	co, _ := app.DB.Where("Name=?", Name).And(" ZuoBiaoID=?", ZuoBiaoID).Get(dataVote)
	fmt.Println(co)
	if co {
		in.Json500(r, "你已投票，无法再次投票")
		return
	}

}

//生成投票二维码(byte)
func VoteQrCode(Name string, ID int) []byte {
	var png []byte
	png, err := qrcode.Encode("http://127.0.0.1:5567/api/DaPingVoteUpdate?ID="+strconv.Itoa(ID)+"&Name="+Name, qrcode.Medium, 256)
	if err != nil {
		in.Exit(err)
	}
	return png
}

//生成投票二维码图片
func VoteQrCodeImg(Name string, ID int) {
	//var png []byte
	key := strconv.Itoa(ID) + strconv.FormatInt(time.Now().Unix(), 10) //用户姓名+redis创建的时间合成key值
	err := qrcode.WriteFile("http://127.0.0.1:5567/api/DaPingVoteUpdate?ID="+strconv.Itoa(ID)+"&Name="+Name, qrcode.Medium, 256, "./qrImg/"+key+".png")
	if err != nil {
		in.Exit(err)
	}

}
